﻿namespace Chain.IDAL
{
    using Chain.DBUtility;
    using Chain.Model;
    using System;
    using System.Data;
    using System.Data.SqlClient;
    using System.Text;

    public class MicroWebsiteOrderLogDetail
    {
        public int Add(Chain.Model.MicroWebsiteOrderLogDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("insert into MicroWebsiteOrderLogDetail(");
            builder.Append("MicroOrderID,MicroGoodsID,MicroOrderDetailPrice,MicroOrderDetailPoint,MicroOrderDetailDiscountPrice,MicroOrderDetailNumber)");
            builder.Append(" values (");
            builder.Append("@MicroOrderID,@MicroGoodsID,@MicroOrderDetailPrice,@MicroOrderDetailPoint,@MicroOrderDetailDiscountPrice,@MicroOrderDetailNumber)");
            builder.Append(";select @@IDENTITY");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroOrderID", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4), new SqlParameter("@MicroOrderDetailPrice", SqlDbType.Money, 8), new SqlParameter("@MicroOrderDetailPoint", SqlDbType.Int, 4), new SqlParameter("@MicroOrderDetailDiscountPrice", SqlDbType.Money, 8), new SqlParameter("@MicroOrderDetailNumber", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MicroOrderID;
            cmdParms[1].Value = model.MicroGoodsID;
            cmdParms[2].Value = model.MicroOrderDetailPrice;
            cmdParms[3].Value = model.MicroOrderDetailPoint;
            cmdParms[4].Value = model.MicroOrderDetailDiscountPrice;
            cmdParms[5].Value = model.MicroOrderDetailNumber;
            object single = DbHelperSQL.GetSingle(builder.ToString(), cmdParms);
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Delete(int MicroOrderLogDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteOrderLogDetail ");
            builder.Append(" where MicroOrderLogDetailID=@MicroOrderLogDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroOrderLogDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroOrderLogDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }

        public bool DeleteList(string MicroOrderLogDetailIDlist)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("delete from MicroWebsiteOrderLogDetail ");
            builder.Append(" where MicroOrderLogDetailID in (" + MicroOrderLogDetailIDlist + ")  ");
            return (DbHelperSQL.ExecuteSql(builder.ToString()) > 0);
        }

        public bool Exists(int MicroOrderLogDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) from MicroWebsiteOrderLogDetail");
            builder.Append(" where MicroOrderLogDetailID=@MicroOrderLogDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroOrderLogDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroOrderLogDetailID;
            return DbHelperSQL.Exists(builder.ToString(), cmdParms);
        }

        public DataSet GetList(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select MicroOrderLogDetailID,MicroOrderID,MicroGoodsID,MicroOrderDetailPrice,MicroOrderDetailPoint,MicroOrderDetailDiscountPrice,MicroOrderDetailNumber ");
            builder.Append(" FROM MicroWebsiteOrderLogDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetList(int Top, string strWhere, string filedOrder)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select ");
            if (Top > 0)
            {
                builder.Append(" top " + Top.ToString());
            }
            builder.Append(" MicroOrderLogDetailID,MicroOrderID,MicroGoodsID,MicroOrderDetailPrice,MicroOrderDetailPoint,MicroOrderDetailDiscountPrice,MicroOrderDetailNumber ");
            builder.Append(" FROM MicroWebsiteOrderLogDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            builder.Append(" order by " + filedOrder);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListByPage(string strWhere, string orderby, int startIndex, int endIndex)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("SELECT * FROM ( ");
            builder.Append(" SELECT ROW_NUMBER() OVER (");
            if (!string.IsNullOrEmpty(orderby.Trim()))
            {
                builder.Append("order by T." + orderby);
            }
            else
            {
                builder.Append("order by T.MicroOrderLogDetailID desc");
            }
            builder.Append(")AS Row, T.*  from MicroWebsiteOrderLogDetail T ");
            if (!string.IsNullOrEmpty(strWhere.Trim()))
            {
                builder.Append(" WHERE " + strWhere);
            }
            builder.Append(" ) TT");
            builder.AppendFormat(" WHERE TT.Row between {0} and {1}", startIndex, endIndex);
            return DbHelperSQL.Query(builder.ToString());
        }

        public DataSet GetListSP(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append(" select MicroWebsiteOrderLogDetail.*,MicroWebsiteGoods.MicroGoodsName,MicroWebsiteGoods.MicroGoodsCode,MicroWebsiteGoods.MicroSalePrice ");
            builder.Append(" from MicroWebsiteOrderLogDetail,MicroWebsiteGoods,MicroWebsiteOrderLog");
            builder.Append(" where " + strWhere);
            return DbHelperSQL.Query(builder.ToString());
        }

        public int GetMaxId()
        {
            return DbHelperSQL.GetMaxID("MicroOrderLogDetailID", "MicroWebsiteOrderLogDetail");
        }

        public Chain.Model.MicroWebsiteOrderLogDetail GetModel(int MicroOrderLogDetailID)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select  top 1 MicroOrderLogDetailID,MicroOrderID,MicroGoodsID,MicroOrderDetailPrice,MicroOrderDetailPoint,MicroOrderDetailDiscountPrice,MicroOrderDetailNumber from MicroWebsiteOrderLogDetail ");
            builder.Append(" where MicroOrderLogDetailID=@MicroOrderLogDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroOrderLogDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = MicroOrderLogDetailID;
            Chain.Model.MicroWebsiteOrderLogDetail detail = new Chain.Model.MicroWebsiteOrderLogDetail();
            DataSet set = DbHelperSQL.Query(builder.ToString(), cmdParms);
            if (set.Tables[0].Rows.Count <= 0)
            {
                return null;
            }
            if ((set.Tables[0].Rows[0]["MicroOrderLogDetailID"] != null) && (set.Tables[0].Rows[0]["MicroOrderLogDetailID"].ToString() != ""))
            {
                detail.MicroOrderLogDetailID = int.Parse(set.Tables[0].Rows[0]["MicroOrderLogDetailID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroOrderID"] != null) && (set.Tables[0].Rows[0]["MicroOrderID"].ToString() != ""))
            {
                detail.MicroOrderID = int.Parse(set.Tables[0].Rows[0]["MicroOrderID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroGoodsID"] != null) && (set.Tables[0].Rows[0]["MicroGoodsID"].ToString() != ""))
            {
                detail.MicroGoodsID = int.Parse(set.Tables[0].Rows[0]["MicroGoodsID"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroOrderDetailPrice"] != null) && (set.Tables[0].Rows[0]["MicroOrderDetailPrice"].ToString() != ""))
            {
                detail.MicroOrderDetailPrice = decimal.Parse(set.Tables[0].Rows[0]["MicroOrderDetailPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroOrderDetailPoint"] != null) && (set.Tables[0].Rows[0]["MicroOrderDetailPoint"].ToString() != ""))
            {
                detail.MicroOrderDetailPoint = int.Parse(set.Tables[0].Rows[0]["MicroOrderDetailPoint"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroOrderDetailDiscountPrice"] != null) && (set.Tables[0].Rows[0]["MicroOrderDetailDiscountPrice"].ToString() != ""))
            {
                detail.MicroOrderDetailDiscountPrice = decimal.Parse(set.Tables[0].Rows[0]["MicroOrderDetailDiscountPrice"].ToString());
            }
            if ((set.Tables[0].Rows[0]["MicroOrderDetailNumber"] != null) && (set.Tables[0].Rows[0]["MicroOrderDetailNumber"].ToString() != ""))
            {
                detail.MicroOrderDetailNumber = int.Parse(set.Tables[0].Rows[0]["MicroOrderDetailNumber"].ToString());
            }
            return detail;
        }

        public int GetRecordCount(string strWhere)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("select count(1) FROM MicroWebsiteOrderLogDetail ");
            if (strWhere.Trim() != "")
            {
                builder.Append(" where " + strWhere);
            }
            object single = DbHelperSQL.GetSingle(builder.ToString());
            if (single == null)
            {
                return 0;
            }
            return Convert.ToInt32(single);
        }

        public bool Update(Chain.Model.MicroWebsiteOrderLogDetail model)
        {
            StringBuilder builder = new StringBuilder();
            builder.Append("update MicroWebsiteOrderLogDetail set ");
            builder.Append("MicroOrderID=@MicroOrderID,");
            builder.Append("MicroGoodsID=@MicroGoodsID,");
            builder.Append("MicroOrderDetailPrice=@MicroOrderDetailPrice,");
            builder.Append("MicroOrderDetailPoint=@MicroOrderDetailPoint,");
            builder.Append("MicroOrderDetailDiscountPrice=@MicroOrderDetailDiscountPrice,");
            builder.Append("MicroOrderDetailNumber=@MicroOrderDetailNumber");
            builder.Append(" where MicroOrderLogDetailID=@MicroOrderLogDetailID");
            SqlParameter[] cmdParms = new SqlParameter[] { new SqlParameter("@MicroOrderID", SqlDbType.Int, 4), new SqlParameter("@MicroGoodsID", SqlDbType.Int, 4), new SqlParameter("@MicroOrderDetailPrice", SqlDbType.Money, 8), new SqlParameter("@MicroOrderDetailPoint", SqlDbType.Int, 4), new SqlParameter("@MicroOrderDetailDiscountPrice", SqlDbType.Money, 8), new SqlParameter("@MicroOrderDetailNumber", SqlDbType.Int, 4), new SqlParameter("@MicroOrderLogDetailID", SqlDbType.Int, 4) };
            cmdParms[0].Value = model.MicroOrderID;
            cmdParms[1].Value = model.MicroGoodsID;
            cmdParms[2].Value = model.MicroOrderDetailPrice;
            cmdParms[3].Value = model.MicroOrderDetailPoint;
            cmdParms[4].Value = model.MicroOrderDetailDiscountPrice;
            cmdParms[5].Value = model.MicroOrderDetailNumber;
            cmdParms[6].Value = model.MicroOrderLogDetailID;
            return (DbHelperSQL.ExecuteSql(builder.ToString(), cmdParms) > 0);
        }
    }
}

